﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class PlaneHp : NetworkBehaviour
{
    [SyncVar(hook = "UpdateHp")]
    private float planeHp;
    [SyncVar(hook = "UpdateMBHp")]
    private float mbHp;
    public Slider localHpSlider, mbHpSlider;
    public Canvas localCanvas;
    private PlaneController controller;

    private void Start()
    {
        controller = GetComponent<PlaneController>();
        planeHp = 100;
        mbHpSlider.value = 1;
        localHpSlider.value = 1;
        if (!isLocalPlayer)
        {
            localCanvas.enabled = false;
            mbHpSlider.enabled = false;
        }
    }

    private void Update()
    {
        if (planeHp <= 0)
        {
            controller.IsDead = true;
        }
    }

    public float PlayerHp
    {
        get
        {
            return planeHp;
        }
        set
        {
            planeHp = value;
        }
    }

    public float MotherBaseHp
    {
        get
        {
            return mbHp;
        }
    }

    public void TakeDamage(float damage)
    {
        if (!isServer)
        {
            return;
        }
        planeHp -= damage;
    }

    private void UpdateHp(float health)
    {
        localHpSlider.value = health / 100;
    }

    public void UpdateMBHp(float motherBaseHp)
    {
        mbHp = motherBaseHp;
        mbHpSlider.value = motherBaseHp / 100;
    }
}